
//首页路由
const CONSTANTS = require('../tools/constants');
const router = require('koa-router')();

const moment = require('moment');

router.get('/', async (ctx) => {
    global.log.debug("访问首页");
    ctx.body = {
        code : CONSTANTS.success_code,
        data : {
            text :"hello world"
        }
    };

})

router.get('/getTime', async (ctx) => {
    log.debug("收到获取服务器时间的请求");
    try {
        ctx.body = {
            code: CONSTANTS.success_code,
            data:  {
                nowTime :moment().format("YYYYMMDDHHmmss"),
            },
        }
    } catch(error) {
        log.error("获取服务器时间失败 ,error = ",error);
    }
    
})
router.get('/getNum',async(ctx)=>{
    log.debug("收到获取访问量的请求");
    let res = await instance.home.getNum();
    ctx.body = {
        code :200,
        data  : {
            num:res
        }
    };
})

router.get('/getHot',async(ctx)=>{
    log.debug("收到获取最新热点的请求");
    let res = await instance.home.getHot();
    for(let i=0;i<res.length;i++) {
        res[i].createTime = '创建于:' + moment(res[i].createTime,"YYYYMMDDHHmmss").format('YYYY-MM-DD HH:mm:ss')

    }
    ctx.body = {
        code :200,
        data  : {
            hot:res
        }
    };
})

router.get('/getJoke',async(ctx)=>{
    log.debug("收到获取冷笑话的请求");
    try {
        let res = await instance.home.getJoke();
        ctx.body = {
            code:200,
            data:res,
        }
    } catch(error) {
        console.log("error->",error);
        ctx.body = error;
    }
    
})

module.exports = router.routes();